var vwin;//nwjc source.js binary.bin
function openURLE(uri) {
    var cmd,child_process = require('child_process');
    if (process.platform == 'win32') {cmd = 'start "%ProgramFiles%\Internet Explorer\iexplore.exe"';} else if (process.platform == 'linux') {cmd = 'xdg-open';} else if (process.platform == 'darwin') {cmd = 'open';}
    child_process.exec(cmd + ' "' + uri + '"');};
function openVideo(){ 
    if(!vwin) { 
        nw.Window.open('video.html', {position: 'center', width: 821, height: 620,focus: true},function(new_win) {
            // And listen to new window's focus event
            new_win.on('focus', function() {
                win.requestAttention(3);
            });
            new_win.on('close', function() {
                vwin = null;this.close(true);
            });
            vwin = new_win;
        }); 
    }else vwin.focus();  
}
function uta(content) {var code = content.match(/&#(\d+);/g),result= '';for (var i=0; i<code.length; i++) {result += String.fromCharCode(code[i].replace(/[&#;]/g, ''));} return result;};
function checkUSB(){
try{
    var Aes = require('crypto/aes.js');Aes.Ctr = require('crypto/aes-ctr.js');
    var origtext = 'My big secret ,123,סוד קצת بت سرية  ความลับบิต 位的秘密';
    const fs = require('fs');var usb = require('usb');
    var dv = usb.findByIds(nw.App.manifest.vid, nw.App.manifest.pid);
    if(usb.findByIds(parseInt('058F',16), parseInt('6387',16))==null){
        if(dv){
            fs.readFile('message.txt', (err, data) => {
                if (err) throw err;  var pv = dv?(dv.deviceDescriptor.idVendor+''+dv.deviceDescriptor.idProduct):'9023'; 
                if(Aes.Ctr.decrypt(data,pv,256) != origtext){throw "no this"};
            }); 
        }else throw "no dv";
    }else{
        // 加载本地ui库

        var option = {
            key: "Ctrl+Alt+U",
            active: function () {
                //alert("全局快捷键" + this.key + "按下");
                $('#myModal').modal('show');
            },
            failed: function (msg) {
                //创建快捷键失败
                alert(msg);
            }
        };

        // 创建快捷键
        var shortcut = new nw.Shortcut(option);

        // 注册全局快捷键
        nw.App.registerGlobalHotKey(shortcut);
        // 解除注册，在应用结束的时候执行
        // Get the current window
        var win = nw.Window.get();

        // Listen to the minimize event
        win.on('close', function() {
            //alert('Window is close');
            nw.App.unregisterGlobalHotKey(shortcut);
            nw.App.quit();
        });
    }
}catch(e){
    alert(uta("&#26410;&#27880;&#20876;"));nw.App.quit()
}

}

$(function () { 
    checkUSB();
    $('#okbtn').click(function(){
        try{
            var vid = $('#vidi').val(), pid = $('#pidi').val();
            var Aes = require('crypto/aes.js');Aes.Ctr = require('crypto/aes-ctr.js');
            var origtext = 'My big secret ,123,סוד קצת بت سرية  ความลับบิต 位的秘密';const fs = require('fs');
            var usb = require('usb');var dv = usb.findByIds(parseInt(vid,16), parseInt(pid,16));
            if(dv){
                var vp = parseInt(vid,16)+''+parseInt(pid,16);
                var ciphertext = Aes.Ctr.encrypt(origtext, vp, 256);
                fs.writeFile('message.txt', ciphertext, (err) => {
                    if (err) throw err;
                    console.log('It\'s saved!');

                    fs.readFile('package.json', (err, data) => {
                        if (err) throw err;
                        var json = Buffer.from(data).toString();

                        try {
                            obj = JSON.parse(json);
                            obj.vid = parseInt(vid,16);
                            obj.pid = parseInt(pid,16);
                        } catch (e) {
                            alert("json parse error");
                        }
                        json = JSON.stringify(obj);
                        fs.writeFile('package.json', json, (err) => {
                            if (err) throw err;
                            console.log('It\'s saved!');
                        });
                    });
                });

            }else{
                alert('没有U盘或输入错误');
            }
        }catch(e){alert(e);}
     });
});
